package com.block.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.IApplicationInfoService;
import com.block.common.util.HttpUtils;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.mapper.AddressInfoMapper;
import com.block.db.mapper.ApplicationInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 应用包管理 服务实现类
 * </p>
 *
 * @author
 * @since 2023-08-05
 */
@Service
public class ApplicationInfoServiceImpl extends ServiceImpl<ApplicationInfoMapper, ApplicationInfo> implements IApplicationInfoService {

    @Resource
    private RedisTemplate<String, ApplicationInfo> redisApplicationInfoTemplate;
    @Resource
    AddressInfoMapper addressInfoMapper;

    private static final String APPINFO_KEY_PREFIX = "APPINFO:APPTYPE:";
    private static final Long APPINFO_KEY_TIME_OUT = 4L;

    @Override
    public ApplicationInfo getAppInfoByAppCode(String appCode) {
        String redisKey = APPINFO_KEY_PREFIX + appCode;
        ApplicationInfo applicationInfo = redisApplicationInfoTemplate.opsForValue().get(redisKey);
        if (ObjectUtil.isEmpty(applicationInfo)) {
            applicationInfo = this.baseMapper.getAppInfoByCode(appCode);
            if (applicationInfo != null) {
                redisApplicationInfoTemplate.opsForValue().set(redisKey, applicationInfo, APPINFO_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return applicationInfo;
    }

    @Override
    public Boolean getPage(String ip, ChannelBaseInfo channelBaseInfo) {
        Boolean flag = false;
        if (!StrUtil.equals(channelBaseInfo.getAppCode(), "hongchang_mini") && !StrUtil.equals(channelBaseInfo.getAppCode(), "xinhui_mini")) {
            return flag;
        }
        Map<String, String> querys = new HashMap<>();
        querys.put("ip", ip);
        String host = "https://kzipglobal.market.alicloudapi.com";
        String path = "/api/ip/query";
        String method = "GET";
        String appcode = "899552fc20174adcbd2cdf63684c97eb";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String body = EntityUtils.toString(response.getEntity());
            JSONObject data = JSON.parseObject(body);
            JSONObject result = data.getJSONObject("data");
            String province = result.getString("province");
            String outCityArray = this.baseMapper.getOutCityArray(channelBaseInfo.getAppCode());
            String cityCode = addressInfoMapper.getProvince(province);
            List<String> outCitylList = JSON.parseArray(outCityArray, String.class);
            if (CollUtil.isNotEmpty(outCitylList) && outCitylList.contains(cityCode)) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public List<String> getCodeByStatus(Integer status) {
        return this.baseMapper.getCodeByStatus(status);
    }


}
